!> author: 左志华
!> date: 2022-07-17
module test_swift_file_m

    use testdrive, only: new_unittest, unittest_type, error_type, check
    use swift_file_m
    implicit none
    private

    public :: collect_swift_file

contains

    !> Collect all exported unit tests
    subroutine collect_swift_file(testsuite)
        !> Collection of tests
        type(unittest_type), allocatable, intent(out) :: testsuite(:)

        testsuite = [ &
                    new_unittest("FUNC: file_name", test_file_name), &
                    new_unittest("FUNC: is_exist", test_is_exist), &
                    new_unittest("FUNC: join", test_join), &
                    new_unittest("FUNC: countlines", test_countlines) &
                    ]

    end subroutine collect_swift_file

    subroutine test_file_name(error)
        type(error_type), allocatable, intent(out) :: error

        open (1, file="example/demo_file_name.f90", status="old")
#ifdef __GFORTRAN__
        !> IFORT 将得到绝对路径的文件名，GFortran 将得到相对路径的文件名
        call check(error, file_name(1), "example/demo_file_name.f90", more="file_name not match")
#endif
        close (1)

    end subroutine test_file_name

    subroutine test_is_exist(error)
        type(error_type), allocatable, intent(out) :: error

        call check(error, is_exist("example/demo_file_name.f90"), .true., more="file_name not exist")
        if (allocated(error)) return

        call check(error, is_exist("example/demo_file_name_not_exist.f90"), .false., more="file_name exist")

    end subroutine test_is_exist

    subroutine test_countlines(error)
        type(error_type), allocatable, intent(out) :: error

        call check(error, countlines("example/demo_file_name.f90"), 9, more="file_name not match")

    end subroutine test_countlines

    impure subroutine test_join(error)
        type(error_type), allocatable, intent(out) :: error

        call check(error, "".join."fpm.toml", "fpm.toml", more="join not match")
        if (allocated(error)) return

        if (is_windows()) then
            call check(error, "example".join."demo_join.f90", "example\demo_join.f90", more="join not match")
        else
            call check(error, "example".join."demo_join.f90", "example/demo_join.f90", more="join not match")
        end if

    end subroutine test_join

end module test_swift_file_m
